﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using VisualWip.Core;
using VisualWip.Core.Interfaces;
using Application = System.Windows.Application;

namespace VisualWip.PresentationModel
{
    public abstract class ColumnProviderViewModelBase : IColumnProviderViewModel
    {
        private readonly IColumnBuilder _columnBuilder;

        [ImportingConstructor]
        protected ColumnProviderViewModelBase(IColumnBuilder columnBuilder)
        {
            _columnBuilder = columnBuilder;
        }

        protected IColumnBuilder ColumnBuilder
        {
            get { return _columnBuilder; }
        }

        public virtual string ResourceDictionaryFileName
        {
            get { return String.Format("{0}.xaml", ProviderName); }
        }

        #region IColumnProviderViewModel Members

        public abstract string ProviderName { get; }
        public abstract string HeaderName { get; }

        public virtual void Init()
        {
            LoadProviderResources();
        }

        public abstract void RegisterViewModels(IViewModelFactory viewModelFactory);

        #endregion

        public virtual void LoadProviderResources()
        {
            using (var fs = new FileStream(ResourceDictionaryFileName, FileMode.Open))
            {
                Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary) XamlReader.Load(fs));
            }
        }
    }
}